package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.goods

import android.os.Bundle
import android.view.View
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.base.BaseLazyDataBindingFragment
import com.example.shushiyunsst.R
import com.example.shushiyunsst.databinding.FragmentDeliveryInfoAgencyBinding
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.bean.AreaItemBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.AddressSpot
import com.lxj.xpopup.XPopup

class DeliveryInfoAgencyFragment : BaseLazyDataBindingFragment<FragmentDeliveryInfoAgencyBinding>(),
    View.OnClickListener {

    companion object {
        fun newInstance(): DeliveryInfoAgencyFragment {
            val args = Bundle()

            val fragment = DeliveryInfoAgencyFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private val selectedAreaLs = mutableListOf<AreaItemBean?>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
    }

    override fun lazyInit() {

    }

    override fun initView(view: View) {
        viewBinder.onViewClick = this
    }


    fun getInfo(): AddressSpot {
        var addressSpot = AddressSpot()
        addressSpot.areaIds = getAreaIds()
        addressSpot.recAddr = getAreaName()
        addressSpot.recName = viewBinder.consignee.text.toString()
        addressSpot.recPhone = viewBinder.contactPhone.text.toString()
        addressSpot = checkInfo(addressSpot)
        return addressSpot
    }

    private fun checkInfo(addressSpot: AddressSpot): AddressSpot {
        if (addressSpot.recAddr.isEmpty()) {
            addressSpot.msgStr = "请输入地址"
        }
        if (addressSpot.recName.isEmpty()) {
            addressSpot.msgStr = "请输入收货人"
        }
        if (addressSpot.recPhone.isEmpty()) {
            addressSpot.msgStr = "请输入联系电话"
        }
        return addressSpot
    }


    private fun getAreaName(): String {
        val deliveryAddress = viewBinder.deliveryAddress22
        if (selectedAreaLs.size <= 0)
            return "$deliveryAddress"

        return "${getSelectLocationName()},$deliveryAddress"
    }

    private fun getAreaIds(): String {
        if (selectedAreaLs.size <= 0) return ""

        var string = StringBuilder()
        for (e in selectedAreaLs) {
            string.append(e?.aid).append(",")
        }

        if (string.lastIndexOf(",") != string.indexOf(",")) {
            string = string.deleteCharAt(string.lastIndexOf(","))
        }

        return string.toString()
    }

    override fun layoutId(): Int {
        return R.layout.fragment_delivery_info_agency
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.select_location -> {
                XPopup.Builder(_mActivity)

                    .asCustom(
                        SelectLocationPop(_mActivity)
                            .setOnSelectListener(object : SelectLocationPop.OnSelectListener {

                                override fun onSelected(ls: List<AreaItemBean?>) {
                                    selectedAreaLs.clear()
                                    selectedAreaLs.addAll(ls)
                                    viewBinder.selectLocation.text = getSelectLocationName()
                                }
                            })
                    )
                    .show()
            }
        }
    }

    private fun getSelectLocationName(): String {

        var string = StringBuilder()
        for (e in selectedAreaLs) {
            string.append(e?.areaName?.replace(",", "")).append(",")
        }

        if (string.lastIndexOf(",") != string.indexOf(",")) {
            string = string.deleteCharAt(string.lastIndexOf(","))
        }

        return string.toString()
    }

}